<?php

/**
 * Object/Contrat.php
 */

namespace RHX\Model\Object;

use Zend\Db\Adapter\Adapter;

class Contrat {

    /**
     *
     * @var int $id
     */
    public $id;

    /**
     *
     * @var int $type_id
     */
    public $type_id;

    /**
     *
     * @var string
     */
    public $libelle;

    /**
     *
     * @var string
     */
    public $debut;

    /**
     *
     * @var string
     */
    public $fin;

    /**
     *
     * @var int
     */
    public $motif_depart;

    /**
     *
     * @var Adapter
     */
    private $adapter;

    /**
     * Constructeur
     *
     * @param \Zend\Db\Adapter\Adapter $adapter
     * @param array $data
     */
    public function __construct(Adapter $adapter, $data = array()) {
	$this->adapter = $adapter;
	$this->exchangeArray($data);
    }

    /**
     * Remplissage des données
     * 
     * @param array $data
     */
    public function exchangeArray($data = array()) {
	$this->id		 = isset($data['id']) ? (int) $data['id'] : null;
	$this->type_id		 = isset($data['type_id']) ? (int) $data['type_id'] : 0;
	$this->libelle		 = isset($data['libelle']) ? $data['libelle'] : '';
	$this->debut		 = isset($data['debut']) ? $data['debut'] : '0000-00-00';
	$this->fin		 = isset($data['fin']) ? $data['fin'] : '0000-00-00';
	$this->motif_depart	 = isset($data['motif_depart']) ? (int) $data['motif_depart'] : 0;
    }

    /**
     * Récupérer les avenants d'un contrat
     *
     * @return ResultSet
     */
    public function getAvenants() {
	// table Avenant
	$tableAvenant	 = new \RHX\Model\Table\AvenantTable($this->adapter);
	$contratId	 = $this->id;
	$avenants	 = $tableAvenant->select(function(\Zend\Db\Sql\Select $select)use($contratId) {
		    $where = new \Zend\Db\Sql\Where();
		    $where->equalTo('contrat_id', $contratId);
		    $select->where($where);
		    $select->order('id ASC');
		});

	return $avenants;
    }

    /**
     * Récupérer l'avenant en cours
     * 
     * @return Avenant
     */
    public function getAvenant() {
	return $this->getAvenants()
			->current();
    }

    /**
     * Type du contrat
     *
     * @return \RHX\Model\Object\ContratType
     */
    public function getContratType() {
	// table contrat type
	$tableType	 = new \RHX\Model\Table\ContratTypeTable($this->adapter);
	$type		 = null;
	try {
	    $type = $tableType->get($this->type_id);
	}
	catch (\Exception $e) {
	    $type = new ContratType();
	}

	return $type;
    }

    /**
     * Motif de départ
     * 
     * @return \RHX\Model\Object\Motif
     */
    public function getMotifDepart() {
	// table motif
	$tableMotif	 = new \RHX\Model\Table\MotifTable($this->adapter);
	$motif		 = null;
	try {
	    $motif = $tableMotif->get($this->motif_depart);
	}
	catch (\Exception $ex) {
	    $motif = new Motif();
	}

	return $motif;
    }

}

?>
